package model;

import protocol.JT905_2014.*;
import utils.ExceptionUtils;
import utils.HexUtils;
import utils.StringUtils;
import java.util.List;

/**
 * JT/T 905.2 - 2014
 */
public class Protocol905_2014Model extends IModel {

    private final static int Protocol_Analysis = 0;//数据内容解析
    private final static int Protocol_Check = 1;//数据校验

    @Override
    protected void initTypes(List<String> list) {
        list.add(Protocol_Analysis,"数据内容解析");
        list.add(Protocol_Check,"数据校验");
    }

    @Override
    public String change(String input) {
        try{
            input = StringUtils.formatProtocol(input);

            byte[] list = HexUtils.toHexBytes(input);
            JT_905_2014Cmd cmd = new JT_905_2014Cmd(list);

            if (getType() == Protocol_Check){
                return checkProtocolStr(cmd,list);
            }

            byte order = cmd.order();

            switch (order) {
                case 0x00:
                    return new Handle905_2014JT00().handle(cmd);
                case 0x01:
                    return new Handle905_2014JT01().handle(cmd);
                case 0x02:
                    return new Handle905_2014JT02().handle(cmd);
                case 0x03:
                    return new Handle905_2014JT03().handle(cmd);
                case 0x05:
                    return new Handle905_2014JT05().handle(cmd);
                case 0x08:
                    return new Handle905_2014JT08().handle(cmd);
                case 0x0B:
                    return new Handle905_2014JT0B().handle(cmd);
                case (byte) 0x80:
                    return new Handle905_2014JT80().handle(cmd);
                case (byte) 0x81:
                    return new Handle905_2014JT81().handle(cmd);
                case (byte) 0x82:
                    return new Handle905_2014JT82().handle(cmd);
                case (byte) 0x83:
                    return new Handle905_2014JT83().handle(cmd);
                case (byte) 0x84:
                    return new Handle905_2014JT84().handle(cmd);
                case (byte) 0x85:
                    return new Handle905_2014JT85().handle(cmd);
                case (byte) 0x88:
                    return new Handle905_2014JT88().handle(cmd);
                case (byte) 0x8B:
                    return new Handle905_2014JT8B().handle(cmd);
                default:
                    return "未匹配的命令字" + (cmd.readOrder() + "," + cmd.readSubOrder());
            }

        }catch (Exception e){
            return "数据校验失败\n"+ ExceptionUtils.getExceptionMsg(e);
        }
    }

    private String checkProtocolStr(JT_905_2014Cmd cmd, byte[] data) {
        StringBuilder builder = new StringBuilder();

        if (data[0] != 0x7e){
            builder.append("数据包头错误(正常包头：7e)："+HexUtils.toHexStr(new byte[]{data[0],data[1]}));
            builder.append("\r\n");
            return builder.toString();
        }

        if (data[data.length - 1] != 0x7e){
            builder.append("数据包尾错误(正常包尾：7e)："+HexUtils.toHex(data[data.length - 1],1));
            builder.append("\r\n");
            return builder.toString();
        }

        builder.append("命令字："+cmd.readOrder()+";"+cmd.readSubOrder());
        builder.append("\r\n");

        builder.append("流水号："+cmd.seq());
        builder.append("\r\n");

        builder.append("数据区："+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }

}
